<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <my-grandfather></my-grandfather>
    </div>

    <!-- 爷爷组件 -->
    <template id="greadfather">
        <div>
            <h1>我是爷爷组件{{count}}</h1>
            <button @click="send">传递</button>
            <my-father></my-father>

        </div>
    </template>

    <!-- 父亲组件 -->
    <template id="father">
        <div>
            <h3>父亲组件</h3>
            <my-son></my-son>
        </div>
    </template>

    <!-- 孙子组件 -->
    <template id="son">
        <div>
            <h5>我是孙子组件{{count}}</h5>
        </div>
    </template>

    <script src="./vue.js"></script>
    <script>

        //事件总线
        const bus=new Vue()


        //孙子
        const Son = {
            template: "#son",
            mounted(){
                //bus.$on(事件名称,)
                bus.$on("send",(data)=>{
                    this.count=data
                })
            },
            data(){
                return{
                    count:null
                }
            },
            beforeDestory(){
                //清空，因为$on是挂起的状态
            }
        }
        //父亲
        const Father = {
            template: "#father",
            components: {
                "my-son": Son
            }
        }
        //爷爷
        const GrandFather = {
            template: "#greadfather",
            components: {
                "my-father": Father
            },
            data(){
                return {
                    count:888
                }
            },
            methods:{
                send(){
                    //bus.$emit(事件名称，传递的数据)
                    bus.$emit("send",this.count)
                }
            }
        }

        new Vue({
            el: "#app",
            components: {
                "my-grandfather": GrandFather
            }
        })
    </script>
</body>

</html>